home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / PRG / MacPerl 506 appl folder.sit / MacPerl 506 appl folder / Mac_Perl_506r1m_appl / lib / finddepth.pl < prev    next >
Text File  |  1995-03-20  |  3KB  |  113 lines

  1. # Usage:
  2. #    require "finddepth.pl";
  3. #
  4. #    &finddepth('/foo','/bar');
  5. #
  6. #    sub wanted { ... }
  7. #        where wanted does whatever you want.  $dir contains the
  8. #        current directory name, and $_ the current filename within
  9. #        that directory.  $name contains "$dir/$_".  You are cd'ed
  10. #        to $dir when the function is called.  The function may
  11. #        set $prune to prune the tree.
  12. #
  13. # This library is primarily for find2perl, which, when fed
  14. #
  15. #   find2perl / -name .nfs¥* -mtime +7 -exec rm -f {} ¥; -o -fstype nfs -prune
  16. #
  17. # spits out something like this
  18. #
  19. #    sub wanted {
  20. #        /^¥.nfs.*$/ &&
  21. #        (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
  22. #        int(-M _) > 7 &&
  23. #        unlink($_)
  24. #        ||
  25. #        ($nlink || (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_))) &&
  26. #        $dev < 0 &&
  27. #        ($prune = 1);
  28. #    }
  29.  
  30. sub finddepth {
  31.     chop($cwd = `pwd`);
  32.     foreach $topdir (@_) {
  33.     (($topdev,$topino,$topmode,$topnlink) = stat($topdir))
  34.       || (warn("Can't stat $topdir: $!¥n"), next);
  35.     if (-d _) {
  36.         if (chdir($topdir)) {
  37. #        ($fixtopdir = $topdir) =~ s,/$,, ;
  38.         ($fixtopdir = $topdir) =~ s,:$,, ;
  39.         &finddepthdir($fixtopdir,$topnlink);
  40. #        ($dir,$_) = ($fixtopdir,'.');
  41.         ($dir,$_) = ($fixtopdir,':');
  42.         $name = $fixtopdir;
  43.         &wanted;
  44.         }
  45.         else {
  46.         warn "Can't cd to $topdir: $!¥n";
  47.         }
  48.     }
  49.     else {
  50. #        unless (($dir,$_) = $topdir =~ m#^(.*/)(.*)$#) {
  51. #        ($dir,$_) = ('.', $topdir);
  52.         unless (($dir,$_) = $topdir =~ m#^(.*:)(.*)$#) {
  53.         ($dir,$_) = (':', $topdir);
  54.         }
  55.         chdir $dir && &wanted;
  56.     }
  57.     chdir $cwd;
  58.     }
  59. }
  60.  
  61. sub finddepthdir {
  62.     local($dir,$nlink) = @_;
  63.     local($dev,$ino,$mode,$subcount);
  64.     local($name);
  65.  
  66.     # Get the list of files in the current directory.
  67.  
  68. #    opendir(DIR,'.') || warn "Can't open $dir: $!¥n";
  69.     opendir(DIR,':') || warn "Can't open $dir: $!¥n";
  70.     local(@filenames) = readdir(DIR);
  71.     closedir(DIR);
  72.  
  73. #    if ($nlink == 2) {        # This dir has no subdirectories.
  74. #    for (@filenames) {
  75. #        next if $_ eq '.';
  76. #        next if $_ eq '..';
  77. #        $name = "$dir/$_";
  78. #        $nlink = 0;
  79. #        &wanted;
  80. #    }
  81. #    }
  82. #    else {                    # This dir has subdirectories.
  83. #    $subcount = $nlink - 2;
  84.     for (@filenames) {
  85. #        next if $_ eq '.';
  86. #        next if $_ eq '..';
  87.         $nlink = $prune = 0;
  88. #        $name = "$dir/$_";
  89.         $name = "$dir:$_";
  90. #        if ($subcount > 0) {    # Seen all the subdirs?
  91.  
  92.         # Get link count and check for directoriness.
  93.  
  94.         ($dev,$ino,$mode,$nlink) = lstat($_) unless $nlink;
  95.         
  96.         if (-d _) {
  97.  
  98.             # It really is a directory, so do it recursively.
  99.  
  100.             if (!$prune && chdir $_) {
  101.             &finddepthdir($name,$nlink);
  102. #            chdir '..';
  103.             chdir '::';
  104.             }
  105. #            --$subcount;
  106.         }
  107. #        }
  108.         &wanted;
  109.     }
  110. #    }
  111. }
  112. 1;
  113.